package com.cyy.common.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;


public class GeneratorUtils {

    private static final String URL = "jdbc:mysql://localhost:3306/c-blog?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";

    private static final String USERNAME = "root";

    private static final String PASSWORD = "CAO201818";

    private static final String PROJECT_PATH = System.getProperty("user.dir");

    //父类地址
    private static final String COMMON_ENTITY = "com.cyy.common.domain.BaseEntity";

    // 父类公共字段
    private static final String[] COMMON_FIELD = {"update_by", "update_time", "create_by", "create_time", "id", "status"};


//    public static void main(String[] args) {
//        autoGenerator("曹云友",
//                "",
//                "friend_link", true);
//    }


    /**
     * 自动生成
     *
     * @param author
     * @param tablePrefix
     * @param taleName
     */
    public static void autoGenerator(String author, String tablePrefix, String taleName, Boolean extendSuper) {

        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            // 关闭 打开文件夹
                            .disableOpenDir()
                            .outputDir(PROJECT_PATH + "/common/src/main/java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    // 设置父包名
                    builder.parent("com.cyy")
                            // 设置父包模块名
                            .moduleName("common")
                            // 设置mapperXml生成路径
                            .entity("pojo.entity")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, PROJECT_PATH + "/common/src/main/resources/mapper"));
                })
                .strategyConfig(builder -> {
                    // 设置需要生成的表名
                    builder.addInclude(taleName)
                            // 设置过滤表前缀
                            .addTablePrefix(tablePrefix);
                    builder.entityBuilder()
                            .superClass(extendSuper ? COMMON_ENTITY : null)
                            .enableLombok()
//                            .enableChainModel()
                            .naming(NamingStrategy.underline_to_camel)
                            .addSuperEntityColumns(extendSuper ? COMMON_FIELD : new String[]{});
                    builder.serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl");
                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }


}